package com.huawei.hwclouds.lubanops.demo.shoppingcart.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huawei.hwclouds.lubanops.demo.common.biz.PageList;
import com.huawei.hwclouds.lubanops.demo.common.json.JsonUtil;
import com.huawei.hwclouds.lubanops.demo.common.model.ErrorMessage;
import com.huawei.hwclouds.lubanops.demo.shopping.cart.biz.model.ShoppingCartsModel;
import com.huawei.hwclouds.lubanops.demo.shopping.cart.biz.param.ShoppingCartsParam;
import com.huawei.hwclouds.lubanops.demo.shopping.cart.biz.service.ShoppingCartService;

@RestController
@RequestMapping(value = "/apm2/demo/shopping/cart/", produces = "application/json; charset=utf-8")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查询
     *
     * @param param
     * @return
     */
    @PostMapping(value = "")
    public PageList<ShoppingCartsModel> getProducts(@RequestBody ShoppingCartsParam param) {
        return shoppingCartService.pageQuery(param);
    }

    /**
     * 购物车结算
     *
     * @param models
     */
    @PostMapping(value = "/settle/accounts")
    public ErrorMessage settleAccounts(@RequestBody List<ShoppingCartsModel> models) {
        shoppingCartService.settleAccounts(models);
        ErrorMessage errorMessage = new ErrorMessage();
        errorMessage.setErrorMsg("执行成功");
        errorMessage.setHint("无");
        return errorMessage;
    }

    @PostMapping("/append")
    public ErrorMessage addProducts(@RequestBody ShoppingCartsModel model) {
        shoppingCartService.create(model);
        ErrorMessage errorMessage = new ErrorMessage();
        errorMessage.setErrorMsg("执行成功");
        errorMessage.setHint("无");
        return errorMessage;
    }

    @PostMapping("/shoppingCarts")
    public List<ShoppingCartsModel> queryShoppingCarts(@RequestBody ShoppingCartsParam param) {
        return shoppingCartService.queryByUserId(param.getUserId());
    }

    @GetMapping("/clear")
    public ErrorMessage clear() {
        shoppingCartService.clear();
        ErrorMessage errorMessage = new ErrorMessage();
        errorMessage.setErrorMsg("执行成功");
        errorMessage.setHint("无");
        return errorMessage;
    }

    @GetMapping("/error/{id}")
    public String error(@PathVariable("id") int id) {
        ShoppingCartsModel shoppingCartsModel = shoppingCartService.errorQuery(id);
        return JsonUtil.toJSONString(shoppingCartsModel);
    }

}
